<?php
/**
 * 获取用户菜单
 * User: Administrator
 * Date: 2018\9\17 0017
 * Time: 16:28
 */

namespace app\Lib\Model\AdminAuth\GetAuthorization;


use app\Lib\InterfaceIn\AdminAuth\GetAuthorization\GetUserAuthMenuHandle;
use app\Lib\InterfaceIn\AdminAuth\MenuAuthHandle;
use app\Lib\InterfaceIn\User\UserHandle;

class GetUserAuthMenu implements GetUserAuthMenuHandle
{
    /** @var UserHandle $userObj  */
    private $userObj;
    /**
     * 初始化
     * GetUserAuthMenuHandle constructor.
     * @param UserHandle $userObj
     */
    public function __construct(UserHandle $userObj)
    {
        $this->userObj = $userObj;
    }

    /**
     * 执行
     * @return array
     */
    public function run()
    {
        $result   =  (new GetAuthorization($this->userObj))->run();
        $menuData = $this->getHandleArray($result);
        return $menuData;
    }

    private function getHandleArray(&$result){
        $data = [];
        foreach ($result as $v){
            /**@var MenuAuthHandle $v  */
            $parentId =$v->getParentClass()->getMenuId();
            $parentId =empty($parentId) ||$parentId == '无' ?0:$parentId;
            $array = [
                'title'=>$v->getName(),
                'icon' =>'',
                'href' =>$v->getProject().'/'.$v->getC().'/'.$v->getA(),
                'spread'=>false,
                'id'=>$v->getMenuId(),
                'level'=>$v->getLevel(),
                'pid'=>$parentId,
                'mark'=>$v->getMark(),
            ];
            $data[]=$array;
        }
        return $data;
    }
}